package com.jk.libsm.mapper;

import com.jk.libsm.pojo.Borrowandreturn;
import com.jk.libsm.vo.Books;
import com.jk.libsm.vo.Userinfo;
import org.apache.ibatis.annotations.*;

import java.util.ArrayList;

/**
 * @author lk
 * @date 2020/7/6 0006 23:16
 * @description:
 */
@Mapper
public interface AdminMapper {
    //按照编号删除图书
    @Delete("delete from book where BNo=#{book_id}")
    void deletebook(long book_id);

    //查询全部用户
    @Select("select RNo,RName,ReaderPhonenumber,RSex,ReaderAddress,ReaderPermission from reader")
    ArrayList<Userinfo> getAlluser();

    //按用户名查询特定用户
    @Select("select RNo,RName,ReaderPhonenumber,RSex,ReaderAddress,ReaderPermission from reader where RNo=#{id}")
    ArrayList<Userinfo> getUserById(String id);

    //更改用户权限
    @Update("update reader set ReaderPermission=#{in} where RNo=#{id}")
    void setReaderPermission(@Param("in") int in,@Param("id") String id);

    // 根据用户id查询用户密码
    @Select("select BAPassword from bookadmin where BANo=#{id}")
    String getUserPasswordById(String id);

    //可借图书
    @Select("select DISTINCT  BNo,BName,Bauthor,Bpublic,BKName,position from book,bookkind where isborrow=1")
    ArrayList<Books> getAllBooks();

    //管理员给用户借书
    @Delete("delete from appoinment where RNo=#{id} and BNo=#{book_id}")
    void deleteAppoinment(@Param("id")String token,@Param("book_id")int book_id);

    @Insert("insert into borrowandreturn (RNo,BNo,borrowdate,shouldreturndate,isreturnbook,reborrow_state) values (#{RNo},#{BNo},#{borrowdate},#{shouldreturndate},#{isreturnbook},#{reborrow_state})")
    void insertBorrow(Borrowandreturn borrowandreturn);

    @Update("update book set borrowCount=borrowCount+1,isappoinment=0,isborrow=0 where book.BNo=#{book_id}")
    void updateBookCount(long book_id);

    //超级管理员给普通管理员注册账号
    @Insert("insert into bookadmin (BANo,BAName,BAPassword,BAPhoneNumber,BAPermission) values (#{id},#{name},#{password},#{phone_num},1)")
    void insertAdmin(@Param("id")String id,@Param("name")String name,@Param("password")String password,@Param("phone_num")String phone_num);
}
